//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.wills.BaseDao;
import com.aidong.dao.wills.PssCheckListHisDao;
import com.aidong.dao.wills.PssLossOrderDao;
import com.aidong.dao.wills.PssLossOrderListDao;
import com.aidong.dao.wills.PssProductStockDao;
import com.aidong.model.wills.CheckProducts;
import com.aidong.model.wills.PssCheckList;
import com.aidong.model.wills.PssCheckListHis;
import com.aidong.model.wills.PssCheckProduct;
import com.aidong.model.wills.PssLossOrder;
import com.aidong.model.wills.PssLossOrderList;
import com.aidong.model.wills.PssProductStock;
import com.aidong.model.wills.PssProductStockPK;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class PssCheckListDao extends BaseDao<PssCheckList, Integer> {
    public PssCheckListDao() {
    }

    public List<PssCheckProduct> queryCheckList(String shopCode) {
        String sql = "select a.ProductCode,a.ProductName,a.Unit,a.price \n,(select isnull(sum(Quantity),0) from PssProductStock where ProductCode=a.ProductCode and ShopCode=:p1) as StockQuantity \n,(select isnull(sum(StockQuantity),0) from PssCheckListHis where ProductCode=a.ProductCode and checkshop=:p2) as StockQuantityHis \n,(select isnull(sum(CheckQuantity),0) from PssCheckListHis where ProductCode=a.ProductCode and checkshop=:p3) as CheckQuantityHis \n,(select isnull(sum(Quantity),0) from PssCheckList where ProductCode=a.ProductCode and CheckShop=:p4) as CheckQuantity \n,isnull((select CheckDate from PssCheckList where ProductCode=a.ProductCode and CheckShop=:p5), \'\') as CheckDate \n,isnull((select CheckDate from PssCheckListHis where ProductCode=a.ProductCode and CheckShop=:p6), \'\') as CheckDateHis \n from PssProductCode a where a.status=\'1\' \n  order by a.ProductCode \n";
        return this.queryListBySql(PssCheckProduct.class, sql, new Object[]{shopCode, shopCode, shopCode, shopCode, shopCode, shopCode});
    }

    public List<PssCheckList> queryCheckListByShopCode(String shopCode) {
        String sql = "select a.ProductCode, a.Quantity \n from PssCheckList a  \n where checkshop=:p1 and Convert(varchar(6),a.CheckDate,112) = convert(nvarchar(6),dateadd(day, -1, Getdate()),112) \n";
        return this.queryListBySql(PssCheckList.class, sql, new Object[]{shopCode});
    }

    public void addCheckList(List<PssCheckList> pssCheckList) {
        Iterator i$ = pssCheckList.iterator();

        while(i$.hasNext()) {
            PssCheckList pcl = (PssCheckList)i$.next();
            PssCheckList tempPcl = this.getByProductCodeAndShopCode(pcl.getProductCode(), pcl.getCheckShop());
            if(null != tempPcl) {
                tempPcl.setQuantity(pcl.getQuantity());
                tempPcl.setCreateUser(pcl.getCreateUser());
                tempPcl.setUpdateTime(pcl.getUpdateTime());
                this.getSession().update(tempPcl);
            } else {
                this.getSession().save(pcl);
            }
        }

    }

    public void addHisAndCheck(List<PssCheckListHis> pssCheckList, PssCheckListHisDao pssCheckListHisDao) {
        PssCheckListHis pcl;
        for(Iterator i$ = pssCheckList.iterator(); i$.hasNext(); pssCheckListHisDao.add(pcl)) {
            pcl = (PssCheckListHis)i$.next();
            PssCheckList tempPcl = this.getByProductCodeAndShopCode(pcl.getProductCode(), pcl.getCheckShop());
            PssCheckListHis tempPclHis = pssCheckListHisDao.getByProductCodeAndShopCode(pcl.getProductCode(), pcl.getCheckShop());
            if(null != tempPcl) {
                this.getSession().delete(tempPcl);
            }

            if(null != tempPclHis) {
                pssCheckListHisDao.delete(tempPclHis);
            }
        }

    }

    public void deleteCheckList(List<PssCheckList> pssCheckList) {
        Iterator i$ = pssCheckList.iterator();

        while(i$.hasNext()) {
            PssCheckList pcl = (PssCheckList)i$.next();
            PssCheckList tempPcl = this.getByProductCodeAndShopCode(pcl.getProductCode(), pcl.getCheckShop());
            if(null != tempPcl) {
                this.getSession().delete(tempPcl);
            }
        }

    }

    public PssCheckList getByProductCodeAndShopCode(String productCode, String shopCode) {
        return (PssCheckList)this.getByWhere("CheckShop=:p1 and ProductCode=:p2", new Object[]{shopCode, productCode});
    }

    public void updateStock(String shopCode, String createUser, CheckProducts products, PssProductStockDao pssProductStockDao, PssLossOrderListDao pssLossOrderListDao, PssLossOrderDao pssLossOrderDao, PssCheckListHisDao pssCheckListHisDao) {
        Date sysdate = new Date();
        PssLossOrder pssLossOrder = new PssLossOrder();
        pssLossOrder.setLossShop(shopCode);
        pssLossOrder.setLossDate(sysdate);
        pssLossOrder.setNote("商品盘点盈亏报损溢");
        pssLossOrder.setCreateUser(createUser);
        pssLossOrder.setCreateTime(sysdate);
        pssLossOrder.setUpdateTime(sysdate);
        pssLossOrderDao.add(pssLossOrder);
        Iterator i$ = products.getLstProduct().iterator();

        while(i$.hasNext()) {
            PssCheckProduct pssCheckProduct = (PssCheckProduct)i$.next();
            PssProductStockPK pssProductStockPK = new PssProductStockPK();
            pssProductStockPK.setProductCode(pssCheckProduct.getProductCode());
            pssProductStockPK.setShopCode(shopCode);
            PssProductStock pssProductStock = (PssProductStock)pssProductStockDao.getById(pssProductStockPK);
            if(pssProductStock == null) {
                pssProductStock = new PssProductStock();
                pssProductStock.setProductCode(pssCheckProduct.getProductCode());
                pssProductStock.setShopCode(shopCode);
                pssProductStock.setQuantity(pssCheckProduct.getDifQuantity());
                pssProductStock.setCreateUser(createUser);
                pssProductStock.setCreateTime(sysdate);
                pssProductStock.setUpdateTime(sysdate);
                pssProductStockDao.add(pssProductStock);
            } else {
                pssProductStock.setQuantity(Double.valueOf(pssProductStock.getQuantity().doubleValue() + pssCheckProduct.getDifQuantity().doubleValue()));
                pssProductStock.setUpdateTime(new Date());
                pssProductStockDao.update(pssProductStock);
            }

            PssLossOrderList pss = new PssLossOrderList();
            pss.setLossId(pssLossOrder.getLossId());
            pss.setProductCode(pssCheckProduct.getProductCode());
            pss.setQuantity(Double.valueOf(-pssCheckProduct.getDifQuantity().doubleValue()));
            pss.setCreateUser(createUser);
            pss.setCreateTime(sysdate);
            pss.setUpdateTime(sysdate);
            pssLossOrderListDao.add(pss);
            PssCheckListHis tempPclHis = pssCheckListHisDao.getByProductCodeAndShopCode(pssCheckProduct.getProductCode(), shopCode);
            if(null != tempPclHis) {
                tempPclHis.setStockQuantity(pssProductStock.getQuantity());
                pssCheckListHisDao.update(tempPclHis);
            }
        }

    }
}
